//
//  ViewController.m
//  SeparableSSS
//
//  Created by yushroom on 8/18/15.
//  Copyright (c) 2015 yushroom. All rights reserved.
//

#import "ViewController.h"
#import "Adapter.h"
#include <mach/mach_time.h>

@interface ViewController ()

@property (nonatomic, strong) EAGLContext *context;
@property (nonatomic) CGPoint startPoint;

- (IBAction)switchSSSSAction:(id)sender;
- (IBAction)printTimeButtonPressed:(id)sender;
- (IBAction)switchBloomAction:(id)sender;
- (IBAction)switchDoFAction:(id)sender;
- (IBAction)switchFilmGrainAction:(id)sender;
- (IBAction)rotateButtonPressed:(id)sender;
- (IBAction)pinch:(UIPinchGestureRecognizer *)sender;

- (void)setupGL;

@end



@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    EAGLContext * context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES3];
    
    if (context == nil)
    {
        context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
    }
    self.context = context;
    
    if (!self.context) {
        NSLog(@"Failed to create ES context");
    }
    
    GLKView *view = (GLKView *)self.view;
    view.context = self.context;
    
    view.drawableColorFormat = GLKViewDrawableColorFormatSRGBA8888;
    view.drawableDepthFormat = GLKViewDrawableDepthFormat24;
    view.drawableStencilFormat = GLKViewDrawableStencilFormat8;
    //view.drawableMultisample = GLKViewDrawableMultisample4X;

    
    self.preferredFramesPerSecond = 60;
    
    [self setupGL];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc
{
    if ([EAGLContext currentContext] == self.context) {
        [EAGLContext setCurrentContext:nil];
    }
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    if ([touches count] > 1)
        return;
    //NSLog(@"touch begin");
    UITouch * touch = [touches anyObject];
    CGPoint point = [touch locationInView:self.view];
    touch_begin(point.x, point.y);
}

- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    if ([touches count] > 1)
        return;
    //NSLog(@"touch move");
    UITouch * touch = [touches anyObject];
    CGPoint point = [touch locationInView:self.view];
    touch_move(point.x, point.y);
}

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    if ([touches count] > 1)
        return;
    //NSLog(@"touch end");
    touch_end();
}

- (IBAction)switchSSSSAction:(id)sender {
    UISwitch * switchButton = (UISwitch*)sender;
    enable_ssss([switchButton isOn]);
}

- (IBAction)printTimeButtonPressed:(id)sender {
    //NSLog(@"%llu", mach_absolute_time());
    //NSLog(@"Total FPS: %ld", (long)self.framesPerSecond);
    print_time();
}

- (IBAction)switchBloomAction:(id)sender {
    UISwitch * switchButton = (UISwitch*)sender;
    enable_bloom([switchButton isOn]);
}

- (IBAction)switchDoFAction:(id)sender {
    UISwitch * switchButton = (UISwitch*)sender;
    enable_dof([switchButton isOn]);
}

- (IBAction)switchFilmGrainAction:(id)sender {
    UISwitch * switchButton = (UISwitch*)sender;
    enable_film_grain([switchButton isOn]);
}

- (IBAction)rotateButtonPressed:(id)sender {
    rotate();
}

- (IBAction)pinch:(UIPinchGestureRecognizer *)sender {
    //NSLog(@"pinch");
    float s = [sender scale];
    s = 1.0f/s;
    s = (s-1) * 0.25f + 1.0f;
    scale(s);
}

- (void)setupGL
{
    [EAGLContext setCurrentContext:self.context];
    
    CGRect screenBound = [[UIScreen mainScreen] bounds];
    CGSize screenSize = screenBound.size;
    glClearColor(1, 0, 0, 1);
    init(screenSize.width * 2, screenSize.height * 2, (GLKView *)self.view);
}

- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect
{
    glClear(GL_COLOR_BUFFER_BIT);
    render();
}

@end
